// store/index.js
import { createStore } from "vuex";
import { wechatLogin } from '../api/api.js'
import dictModule from './modules/dict'  // 导入字典模块

const mutations = {
	SET_POSSIBLE_DATA(state, data) {
		state.possibleData = data;
	},
	SET_ACCESSTOKEN(state, accessToken) {
		state.accessToken = accessToken;
	},
	SET_TOKEN(state, token) {
		state.token = token;
	},
	SET_OPENID(state, openid) {
		state.openid = openid;
	},
	SET_USERINFO(state, data) {
		state.userInfo = data;
	},
};

const state = {
	possibleData: {},
	accessToken: '',
	openid: '',
	token: '',
	userInfo: {}
};

const actions = {
	GetLoginDatas({ commit }, payload: any) {
		const { code, type } = payload;
		return new Promise((resolve, reject) => {
			var newsTarget = {}
			if (type === 'H5') {
				newsTarget = {
					code: code,
					type: type
				}
			}
			if (type === 'codePass') {
				newsTarget = {
					code: 'codePass',
					oid: code,
					type: type
				}
			}
			wechatLogin(newsTarget).then(res => {
				const { accessToken, openid, token } = res.data
				uni.setStorageSync("token", token)
				localStorage.setItem('accessToken', accessToken)
				localStorage.setItem('openid', openid)
				commit('SET_ACCESSTOKEN', accessToken)
				commit('SET_OPENID', openid)
				commit('SET_TOKEN', token)
				resolve(res)
			}).catch(error => {
				reject(error)
			})
		})
	},
};

export default createStore({
	state,
	getters: {},
	mutations,
	actions,
	modules: {
		dict: dictModule
	}
});